<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
class activity extends Controller
{
       public function index(){
       	$activity=Db::name("activity")->select();
       	// dump($shop);exit;
       	$this->assign("activity",$activity);
       	return $this->fetch();
       }
       public function delete($id){
       	//先把对应的商品头像删除
         

       	if(Db::name("activity")->delete($id)) 
       	{
       	   $this->success('删除成功');
       	}else $this->fail('删除失败');
       }
       public function add(){
    return $this->fetch();
}

public function activityadd(){
       $file = request()->file('pic'); 
        //移动到框架应用根目录/public/static/images/ 目录下 
       var_dump($file);
        if($file){
          $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'imageactivity'); 
          if ($info) { 
              $filename=$info->getFilename(); 
          } else { 
              $this->error($file->getError()); 
          } 
        }

    if(request()->isPost()){

      $data=[
        'activityname'=>input('param.activityname'),
        'activitypic'=>$filename,
        'pirce'=>input('param.pirce'),
        'pernem'=>input('pernem'),
        'startdate'=>date('Y-m-d H:i:s'),
        'enddate'=>date('Y-m-d H:i:s'),
        'addres'=>input('param.addres'),
       'adminid'=>1,
        'intro'=>input('param.intro'),
      ];
    }
 
   $res=Db::name("activity")->insert($data);
    if($res){
      $this->redirect('activity/index',5,'新增培训成功');
    }else{
      $this->error("新增培训失败");
    }
    return $this->fetch();
  }

public function edit(){
    $id=input('param.id');
    $activity=Db::name('activity')->find($id);
    $this->assign("activity",$activity);
    return $this->fetch();
}

public function update(){
   $file = request()->file('pic'); 
        //移动到框架应用根目录/public/static/images/ 目录下 
      
        if($file){
          $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'static'. DS . 'imageactivity'); 
          if ($info) { 
              $filename=$info->getFilename(); 
          } else { 
              $this->error($file->getError()); 
          } 
        }
        $c=input('param.activityid');
        var_dump($c);
      
    if(request()->isPost()){
     
      $data=[
        'activityid'=>input('param.activityid'),
        'activityname'=>input('param.activityname'),
        'activitypic'=>$filename,

        'pirce'=>input('param.pirce'),
        'pernem'=>input('pernem'),
        'startdate'=>date('Y-m-d H:i:s'),
        'enddate'=>date('Y-m-d H:i:s'),
        'addres'=>input('param.activityplace'),

        'intro'=>input('param.intro'),
      ];
    }
 
   $res=Db::name("activity")->update($data);
    if($res){
      $this->redirect('activity/index',5,'修改成功');
    }else{
      $this->error("修改失败");
    }
    return $this->fetch();
  }
}